package com.example.administrator.hqq.fragment.discover;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.example.administrator.hqq.R;
import com.example.administrator.hqq.View.GridViewInvalidListener;
import com.example.administrator.hqq.adapter.discover.NewConventionGridAdapter;
import com.example.administrator.hqq.bean.MVPConventionBean;
import com.example.administrator.hqq.bean.MainActivityEventData;
import com.example.administrator.hqq.config.UrlFactory;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import okhttp3.Call;

/**
 * MVP版本的圈子页面
 */
public class MVPConventionFragment extends Fragment {

    private final String TAG = this.getClass().getSimpleName();

    Unbinder unbinder;
    @BindView(R.id.conention_modie)
    GridViewInvalidListener conentionModie;
    private MVPConventionBean mvpConventionBean;
    private List<MVPConventionBean.DataBean> data;
    private NewConventionGridAdapter newConventionGridAdapter;

    public MVPConventionFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_mvpconvention, container, false);
        unbinder = ButterKnife.bind(this, view);
        EventBus.getDefault().register(this);
        initview();
        initdata();
        return view;
    }

    private void initview() {
        data = new ArrayList<>();
        newConventionGridAdapter = new NewConventionGridAdapter(data, getContext());
        conentionModie.setAdapter(newConventionGridAdapter);
        conentionModie.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //向主页发送Eventbus来更新群列表和贴纸fragment
                MainActivityEventData mainActivityEventData = new MainActivityEventData(data.get(i).getId(), 4);
                mainActivityEventData.setTopic_name(data.get(i).getName());
                EventBus.getDefault().post(mainActivityEventData);
            }
        });
    }

    private void initdata() {
        OkHttpUtils.get().url(UrlFactory.getMVPListAllTopic())
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Log.e(TAG, "onError: " + e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                Log.e(TAG, "onResponse: " + response);
                Gson gson = new Gson();
                mvpConventionBean = gson.fromJson(response, MVPConventionBean.class);
                data.addAll(mvpConventionBean.getData());
                newConventionGridAdapter.notifyDataSetChanged();
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(String string) {
  /*      if ("更新常规模块".equals(string))
        {
            initdata();
        }*/
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
    }
}
